package com.dream.libxrec.camera;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.Log;
import android.util.Size;
import android.view.Surface;
import android.view.SurfaceView;
import android.view.TextureView;

import com.luoye.bzyuvlib.BZYUVUtil;

import java.nio.ByteBuffer;

/**
 * 绘制Surface的方案一，相对方案二的效率更高，但是不如方案二实现简单
 */
public class CameraPreviewHelper {
    private Surface mSurface = null;
    private Bitmap mSurfaceBitmap = null;
    private byte[] mRgbaData = null;
    private Matrix mSurfaceMatrix = null;

    private int mWidth = 0;
    private int mHeight = 0;
    private boolean isWaitValid = false;
    private boolean isAllowBack = false;

    /**
     * 设置预览
     * @param surface
     * @param width yuv数据的宽
     * @param height yuv数据的高
     * @param isAllowBack 进入后台是否依旧保留，如果不允许，当Surface被销毁后，预览也会被销毁,
     *                    反之，会等待Surface恢复后继续绘制
     */
    public synchronized void setPreview(Surface surface, int width,
                                        int height,
                                        boolean isAllowBack) {
        if (surface == null){
            mSurface = null;
            if (mSurfaceBitmap != null){
                this.mSurfaceBitmap.recycle();
                this.mSurfaceBitmap = null;
            }

            this.mRgbaData = null;
            this.mSurfaceMatrix = null;
        }else{
            this.isAllowBack = isAllowBack;
            this.mHeight = height;
            this.mWidth = width;

            if (!surface.isValid()){
                isWaitValid = true;
                mSurface = surface;
                return;
            }

            initSurface(surface);
        }
    }

    private void initSurface(Surface surface) {
        this.mSurfaceBitmap = Bitmap.createBitmap(mWidth,
                mHeight,
                Bitmap.Config.ARGB_8888);
        this.mRgbaData =
                new byte[mWidth * mHeight * 4];

        Canvas c = surface.lockHardwareCanvas();
        float scaleX =
                (float) c.getWidth() / mSurfaceBitmap.getWidth();
        float scaleY =
                (float) c.getHeight() / mSurfaceBitmap.getWidth();
        float scale = Math.min(scaleX, scaleY);
        float translateX = (c.getWidth() - mSurfaceBitmap.getWidth() * scale) / 2;
        float translateY = (c.getHeight() - mSurfaceBitmap.getWidth() * scale) / 2;
        //居中及缩放
        Matrix matrix = new Matrix();
        matrix.setScale(scale, scale);
        matrix.postTranslate(translateX, translateY);
        this.mSurfaceMatrix = matrix;
        surface.unlockCanvasAndPost(c);

        mSurface = surface;
    }


    public synchronized void draw(byte[] data,boolean isMirror,int rotation){

        if (mSurface == null){
            return;
        }

        if (!mSurface.isValid()){
            if (!isWaitValid && !isAllowBack) {
                //释放资源
                setPreview(null,0,0,false);
            }

            return;
        }

        try {
            if (isWaitValid){
                isWaitValid = false;
                initSurface(mSurface);
            }

            Canvas c = mSurface.lockHardwareCanvas();

            BZYUVUtil.yuv420ToRGBA(data,mRgbaData,
                    mWidth,mHeight,isMirror,
                    rotation);

            mSurfaceBitmap.copyPixelsFromBuffer(ByteBuffer.wrap(mRgbaData));
            c.drawBitmap(mSurfaceBitmap,mSurfaceMatrix,null);

            //Surface可能会失效，所以需要判断，如果启动了其他Activity
            if (mSurface.isValid()){
                mSurface.unlockCanvasAndPost(c);
            }
        }catch (Exception ignored){}
    }

}
